//
//  CCCommentTableViewCell.m
//  Activity
//
//  Created by its on 15/5/21.
//  Copyright (c) 2015年 coco. All rights reserved.
//

#import "CCCommentTableViewCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <Masonry/Masonry.h>
#import <ReactiveCocoa/ReactiveCocoa.h>
#import "CCComCellViewModel.h"
#import "CCDevice.h"
#import <SECoreTextView/SETextView.h>
@interface CCCommentTableViewCell()
@property (nonatomic, strong) UIImageView *head;
@property (nonatomic, strong) UILabel *author;
@property (nonatomic, strong) UIButton *replay;
@property (nonatomic, strong) SETextView *content;
@property (nonatomic, strong) UIButton *up;
@property (nonatomic, strong) UILabel *upCount;
@property (nonatomic, strong) UIView *seperateLine;
@end
@implementation CCCommentTableViewCell

- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  if ( self = [super initWithStyle: style reuseIdentifier:reuseIdentifier]) {
    [self setupUI];
    [self setupCSS];
  }
  
  return self;
}

- (void)awakeFromNib {
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (void) setupUI {
  _head = [UIImageView new];
  _head.backgroundColor = [UIColor yellowColor];
  _replay = [UIButton buttonWithType:UIButtonTypeCustom];
  [_replay setImage:[UIImage imageNamed:@"comment_normal"] forState:UIControlStateNormal];
  _up = [UIButton buttonWithType:UIButtonTypeCustom];
  [_up setImage:[UIImage imageNamed:@"appreciate_normal"] forState:UIControlStateNormal];
  _upCount = [UILabel new];
  _upCount.textColor = RGB(169, 183, 183, 1);
  _upCount.font = [UIFont systemFontOfSize:14];
  _content = [[SETextView alloc] initWithFrame:CGRectZero];
  _content.backgroundColor = [UIColor whiteColor];
  _author = [UILabel new];
  _author.font = [UIFont systemFontOfSize:14];
  _seperateLine = [UIView new];
  _seperateLine.backgroundColor = RGB(244, 244, 244, 1);
  [self.contentView addSubview:_author];
  [self.contentView addSubview:_head];
  [self.contentView addSubview:_replay];
  [self.contentView addSubview:_up];
  [self.contentView addSubview:_upCount];
  [self.contentView addSubview:_content];
  [self.contentView addSubview:_seperateLine];
}

- (void) setupCSS {
  [self.head mas_updateConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.contentView).offset(5);
    make.leading.equalTo(self.contentView).offset(10);
    make.size.mas_equalTo(CGSizeMake(30, 30));
  }];
  
  [self.author mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(self.head);
    make.right.equalTo(self.up.mas_left).offset(-5);
    make.left.equalTo(self.head.mas_right).offset(4);
  }];
  
  [self.up mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self.upCount.mas_left).offset(-5);
    make.centerY.equalTo(self.head);
    make.width.equalTo(@(20));
    make.height.equalTo(@(20));
  }];
  
  [self.upCount mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self.replay.mas_left).offset(-20);
    make.centerY.equalTo(self.head);
  }];
  
  [self.replay mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self.contentView).offset(-10);
    make.centerY.equalTo(self.head);
    make.width.equalTo(@(20));
    make.height.equalTo(@(20));
  }];
  
  [self.content mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.head.mas_bottom).offset(5);
    make.left.equalTo(self.head.mas_leading);
  }];
  
  [self.seperateLine mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.content.mas_bottom).offset(5);
    make.left.equalTo(self.contentView);
    make.right.equalTo(self.contentView);
    make.height.equalTo(@1);
  }];
  [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(@(0));
    make.top.equalTo(@(0));
    make.bottom.equalTo(self.content).offset(5);
    make.width.equalTo(@(DEVICEWIDTH));
  }];
}

- (void) bindUI {
}

- (void) bindWithViewModel:(CCComCellViewModel *)viewModel {
  [_head sd_setImageWithURL:viewModel.head placeholderImage:nil];
    self.author.attributedText = viewModel.author;
    self.upCount.attributedText = viewModel.upCount;
    self.content.attributedText = viewModel.content;
  CGRect frame = [SETextView frameRectWithAttributtedString:_content.attributedText constraintSize:CGSizeMake(DEVICEWIDTH, CGFLOAT_MAX)];
  [_content mas_updateConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.head.mas_bottom).offset(10);
    make.left.equalTo(self.head);
    make.size.mas_equalTo(CGSizeMake(CGRectGetWidth(frame), CGRectGetHeight(frame)));
  }];
  
  _up.rac_command = [[RACCommand alloc] initWithEnabled:nil signalBlock:^RACSignal *(id input) {
    return [viewModel upAction];
  }];
  _replay.rac_command = [[RACCommand alloc] initWithEnabled:nil signalBlock:^RACSignal *(id input) {
    return [viewModel reply];
  }];
  
  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
  [tap.rac_gestureSignal subscribeNext:^(id x) {
    // 进入个人主页
  }];
  [self.head addGestureRecognizer: tap];

}

+ (CGFloat) getCellHeight:(CCComCellViewModel *) viewModel {
  CGRect frame = [SETextView frameRectWithAttributtedString:viewModel.content constraintSize:CGSizeMake(DEVICEWIDTH, CGFLOAT_MAX)];
  return 5 + 30 + 10 + CGRectGetHeight(frame) + 5;
}

- (void) dealloc {
  
}
@end

